package com.tiancheng.onecode.controller.admin.platform;

import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.model.dto.PlatformMerchantUserDetailDTO;
import com.tiancheng.onecode.model.request.admin.*;
import com.tiancheng.onecode.model.response.PageInfo;
import com.tiancheng.onecode.model.response.Result;
import com.tiancheng.onecode.model.response.admin.*;
import com.tiancheng.onecode.service.IMerchantService;
import com.tiancheng.onecode.service.IPlatformMerchantSettleRuleService;
import com.tiancheng.onecode.utils.MerchantContext;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: kellen
 * @Description: 商户管理 admin controller
 * @Date: create in 2024/12/9 21:27
 */
@Slf4j
@Controller
@RequestMapping("/admin/platform/merchant")
public class PlatformMerchantAdminController {

    @Resource
    private IMerchantService merchantService;
    @Resource
    private IPlatformMerchantSettleRuleService platformMerchantSettleRuleService;

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public Result<PageInfo<MerchantQueryRes>> queryPage(@RequestBody MerchantQueryReq params) {
        PageInfo<MerchantQueryRes> pageInfo = merchantService.queryPage(params);
        return Result.success(pageInfo);
    }

    /**
     * 商户入驻：第一步，添加商户信息
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/addMerchant/v2", method = RequestMethod.POST)
    @ResponseBody
    public Result<MerchantAddStepOneNewRes> addMerchantNew(@RequestBody @Valid MerchantAddReqNew params,HttpServletRequest servletRequest) {
        Asserts.isNotNull(params.getPlatformPercent(), "抽佣金额不能为空!");
        String xToken = servletRequest.getHeader("X-Token");
        log.info("xToken: {}", xToken);
        params.setXToken(xToken);
        final MerchantAddStepOneNewRes add = merchantService.addNew(params);
        return Result.success(add);
    }


    /**
     * 修改结算信息
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateSettlement", method = RequestMethod.POST)
    @ResponseBody
    public Result updateSettlement(@RequestBody @Valid MerchantUpdateSettlReq params) {
        merchantService.updateSettlement(params);
        return Result.success();
    }

    /**
     * 停用清分规则
     *
     * @param platformMerchantId 平台商户关联id
     * @return
     */
    @RequestMapping(value = "/deactivateSettlement", method = RequestMethod.DELETE)
    @ResponseBody
    public Result deactivateSettlement(@RequestParam("platformMerchantId") Long platformMerchantId) {
        merchantService.deactivateSettlement(platformMerchantId);
        return Result.success();
    }

    /**
     * 新增清分规则
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/addSettlementRule", method = RequestMethod.POST)
    @ResponseBody
    public Result addSettlement(@RequestBody @Valid MerchantAddSettlRuleReq params) {
        merchantService.addSettlementRule(params);
        return Result.success();
    }

    /**
     * 根据社会信用代码查询商户信息
     *
     * @param platformCode
     * @param shopLic
     * @return
     */
    @RequestMapping(value = "/queryByShopLic", method = RequestMethod.GET)
    @ResponseBody
    public Result<MerchantAddStepOneRes> queryByShopLic(@RequestParam("platformCode") String platformCode, @RequestParam("shopLic") String shopLic) {
        MerchantAddStepOneRes res = merchantService.queryByShopLic(platformCode, shopLic);
        return Result.success(res);
    }

    /**
     * 根据商户进件id查询状态
     *
     * @param platformMerchantId 平台商户id
     * @return
     */
    @RequestMapping(value = "/queryMerchantStatus", method = RequestMethod.GET)
    @ResponseBody
    public Result<MerchantRegStatusRes> queryMerchantStatus(@RequestParam("platformMerchantId") Long platformMerchantId) {
        MerchantRegStatusRes res = merchantService.queryMerchantStatus(platformMerchantId);
        return Result.success(res);
    }

    /**
     * 根据商户编码查询商户信息
     *
     * @param platformCode
     * @param merchantCode
     * @return
     */
    @RequestMapping(value = "/queryByMerchantCode", method = RequestMethod.GET)
    @ResponseBody
    public Result<MerchantAddStepOneRes> queryByMerchantCode(@RequestParam("platformCode") String platformCode, @RequestParam("merchantCode") String merchantCode) {
        MerchantAddStepOneRes res = merchantService.queryByPlatformCodeAndMerchantCode(platformCode, merchantCode);
        return Result.success(res);
    }

    /**
     * 查询商户基础信息
     *
     * @param platformMerchantId 平台商户关联id
     * @return
     */
    @RequestMapping(value = "/queryMerchantBaseInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result<MerchantAddStepOneRes> queryByMerchantCode(@RequestParam("platformMerchantId") Long platformMerchantId) {
        MerchantAddStepOneRes res = merchantService.queryByPlatformMerchantId(platformMerchantId);
        return Result.success(res);
    }

    /**
     * 查询商户业务信息
     *
     * @param platformMerchantId 平台商户id
     * @return
     */
    @RequestMapping(value = "/queryMerchantBusinessInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result<PlatformMerchantBusinessInfoRes> queryMerchantBusinessInfo(@RequestParam("platformMerchantId") Long platformMerchantId) {
        PlatformMerchantBusinessInfoRes res = merchantService.queryMerchantBusinessInfo(platformMerchantId);
        return Result.success(res);
    }

    /**
     * 查询商户业务信息
     *
     * @param setMerchantAdminReq 平台商户id
     * @return
     */
    @RequestMapping(value = "/changeMerchantAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Result<PlatformMerchantUserDetailDTO> changeMerchantAdmin(@RequestBody @Valid SetMerchantAdminReq setMerchantAdminReq,HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        setMerchantAdminReq.setXToke(token);
        PlatformMerchantUserDetailDTO res = merchantService.changeMerchantAdmin(setMerchantAdminReq);
        return Result.success(res);
    }

    // ----------------------------------------------------------------------------平台商户状态管理--------------------------------------------------------------

    /**
     * 商户状态管理
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/setMerchantStatus/v2", method = RequestMethod.POST)
    @ResponseBody
    public Result setMerchantStatusNew(@RequestBody @Valid MerchantStatusSetReq params, HttpServletRequest servletRequest) {
        String xToken = servletRequest.getHeader("X-Token");
        log.info("xToken: {}", xToken);
        params.setXToken(xToken);
        merchantService.setMerchantStatusNew(params);
        return Result.success();
    }

    /**
     * 更新商户信息
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateMerchantInfo/v2", method = RequestMethod.POST)
    @ResponseBody
    public Result updateMerchantInfo(@RequestBody @Valid MerchantUpdateNewReq params, HttpServletRequest servletRequest) {
        String xToken = servletRequest.getHeader("X-Token");
        log.info("xToken: {}", xToken);
        params.setXToken(xToken);
        merchantService.updateMerchantInfoNew(params);
        return Result.success();
    }


    /**
     * 统计平台商户信息
     *
     * @param platformCode
     * @return
     */
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    @ResponseBody
    public Result<MerchantStatisticsRes> merchantStatistics(@RequestParam(name = "platformCode", required = false) String platformCode) {
        if (StringUtils.isEmpty(platformCode)) {
            platformCode = MerchantContext.getPlatform().getPlatformCode();
        }
        MerchantStatisticsRes result = merchantService.merchantStatistics(platformCode);
        return Result.success(result);
    }

    /**
     * 查询子商户基本信息
     * @param merchantsReq
     * @return
     */
    @RequestMapping(value = "/queryMerchants", method = RequestMethod.POST)
    @ResponseBody
    public Result<List<MerchantSimpleInfoRes>> queryMerchants(@RequestBody @Valid QueryMerchantsReq merchantsReq) {
        /*if (StringUtils.isEmpty(merchantsReq.getPlatformCode())) {
            merchantsReq.setPlatformCode(MerchantContext.getMerchant().getPlatformCode());
        }*/
        merchantsReq.setPlatformCode(MerchantContext.getPlatform().getPlatformCode());
        List<MerchantSimpleInfoRes> result = merchantService.queryMerchantSimpleInfo(merchantsReq);
        return Result.success(result);
    }


    /**
     * 获取最新省市区以及行业数据列表
     * @param dataType 文件类型 province,city,county,mcc可选，分别对应省、市、区和行业数据。
     * @return
     * @throws IOException
     */
    @GetMapping("/regionData")
    public Result<String> upload(@RequestParam("dataType") String dataType) throws IOException {
        //把文件的内容存储到本地磁盘上
        final String url = merchantService.regionData(dataType);
        return Result.success(url);
    }


    /**
     * 商户对公打款验证
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/merchantAccountVerify", method = RequestMethod.POST)
    @ResponseBody
    public Result merchantAccountVerify(@RequestBody @Valid MerchantAccountVerifyReq params) {
        merchantService.merchantAccountVerify(params);
        return Result.success("对公账户验证申请成功，请到对应对公账户查看打款金额！");
    }

    /**
     * 发起对公账户验证交易
     *
     * @param param
     * @return
     */
    @PostMapping("/companyAccountVerify")
    @ResponseBody
    public Result companyAccountVerify(@RequestBody @Validated MerchantCompanyAccountVerifyReq param) {
        merchantService.companyAccountVerify(param);
        return Result.success("对公账户打款金额验证成功！");
    }

    /**
     * 前台签约接口
     *
     * @param param
     * @return
     */
    @PostMapping("/agreementSign")
    @ResponseBody
    public Result<String> agreementSign(@RequestBody @Validated MerchantPayAgreementSignReq param) {
        String url = merchantService.agreementSign(param);
        return Result.success(url);
    }



}
